//
//  StreetAdvisoryViewController.m
//  homeland
//
//  Created by SunShine on 13-11-24.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "StreetAdvisoryViewController.h"
#import "AdvisoryListViewController.h"

#define INPUT_VIEW_OFFSET 200.0

@interface StreetAdvisoryViewController ()
@property (strong, nonatomic) AdvisoryService *dataService;
@end

@implementation StreetAdvisoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.dataService = [AdvisoryService new];
    _dataService.delegate = self;
//    CommonMineQueryItem *item = [_config objectForKey:@"dataItem"];
    if ([self.currentTitle hasPrefix:@"查"]) {
        self.currentTitle = [self.currentTitle substringFromIndex:1];
    }
    self.title = self.currentTitle;
    self.navigationItem.rightBarButtonItem = getCustomNavButton(@"反映记录", self, @selector(rightButtonAction:));
    self.scrollView.contentSize = CGSizeMake(320.0, 500.0);
    _nameField.inputAccessoryView = [self getInputAccessoryView];
    _nameField.delegate = self;
    _phoneField.inputAccessoryView = [self getInputAccessoryView];
    _phoneField.delegate = self;
    _contentTextView.inputAccessoryView = [self getInputAccessoryView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Text Field Delegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y - INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView beginAnimations:@"animation" context:nil];
    [UIView setAnimationDuration:0.2f];
    CGRect frame = self.view.frame;
    frame.origin.y = frame.origin.y + INPUT_VIEW_OFFSET;
    self.view.frame = frame;
    [UIView commitAnimations];
}

#pragma mark - Button
- (IBAction)commitButtonAction:(id)sender
{
    NSString *name = _nameField.text;
    NSString *phone = _phoneField.text;
    NSString *content = _contentTextView.text;
    if (!name || name.length == 0) {
        [self showAlert:@"姓名不能为空."];
        [_nameField becomeFirstResponder];
        return;
    }
    if (!phone || phone.length == 0) {
        [self showAlert:@"电话不能为空."];
        [_phoneField becomeFirstResponder];
        return;
    }
    if (!content || content.length == 0) {
        [self showAlert:@"咨询内容不能为空."];
        [_contentTextView becomeFirstResponder];
        return;
    }
    //调用服务
    [self startLoadingViewWithText:@"正在提交..."];
    [_dataService postAdvisoryWithTitle:nil Name:name Phone:phone Content:content DataItem:[_config objectForKey:@"dataItem"]];
}

-(void)showAlert:(NSString *) msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)rightButtonAction:(id)sender
{
    AdvisoryListViewController *listVC = [[AdvisoryListViewController alloc]initWithNibName:@"AdvisoryListViewController" bundle:nil];
    listVC.config = _config;
    [self.navigationController pushViewController:listVC animated:YES];
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    if (_dataService.dealCode && _dataService.dealCode != (id)[NSNull null] && _dataService.dealCode.length > 0) {
        [self showAlert:[NSString stringWithFormat:@"您提交的信访事项(受理编号%@)已转有关单位办理,您可以凭受理编号与姓名查询办理情况",_dataService.dealCode]];
    }
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
